/**
 * 提示与加载工具类
 */
export default class Tips {
  constructor() {
    this.isLoading = false
  }
  /**
   * 弹出提示检测位置
   */
  static async showLocationModal() {
    uni.showModal({
      content: '检测到您未开启地理位置授权',
      confirmText: '去开启',
      success: function(res) {
        if (res.confirm) {
          
        }
      }
    })
  }

  /**
   * 弹出确认窗口
   */
  static confirm(text, cancel = true, payload = {}, title = '提示') {
    return new Promise((resolve, reject) => {
      uni.showModal({
        title: title,
        content: text,
        showCancel: cancel,
        success: res => {
          if (res.confirm) {
            resolve(payload)
          } else if (res.cancel) {
            reject(payload)
          }
        },
        fail: res => {
          reject(payload)
        }
      })
    })
  }

  /**
   * 弹出提示框
   */

  static async success(title, duration = 500) {
    setTimeout(() => {
      uni.showToast({
        title: title,
        icon: 'success',
        mask: true,
        duration: duration
      })
    }, 300)
    await this.timeout(duration)
  }
  static toast(title, icon = 'success') {
    uni.showToast({
      title: title,
      icon: icon,
			position: 'bottom',
      mask: true
    })
  }

  /**
   * 警告框
   */
  static alert(title) {
    uni.showToast({
      title: title,
      image: '/static/alert.png',
      mask: true
    })
  }

  /**
   * 错误框
   */

  static error(title) {
    uni.showToast({
      title: title,
      image: '/static/error.png',
      mask: true
    })
  }

  /**
   * 弹出加载提示
   */
  static loading(title = '加载中') {
    if (Tips.isLoading) {
      return
    }
    Tips.isLoading = true
    uni.showLoading({
      title: title,
      mask: true
    })
  }

  /**
   * 加载完毕
   */
  static loaded() {
    if (Tips.isLoading) {
      Tips.isLoading = false
      uni.hideLoading()
    }
  }

  static share(title, url, img) {
    return {
      title: title,
      path: url,
      imageUrl: img,
      success: function(res) {
        Tips.toast('分享成功')
      }
    }
  }

  /**
   * 弹出下拉动作栏
   */
  static action (...items) {
    return new Promise((resolve, reject) => {
      uni.showActionSheet({
        itemList: items,
        success: function (res) {
          const result = {
            index: res.tapIndex,
            text: items[res.tapIndex]
          }
          resolve(result)
        },
        fail: function (res) {
          reject(res.errMsg)
        }
      })
    })
  }
  static actionWithFunc (items, ...functions) {
    uni.showActionSheet({
      itemList: items,
      success: function (res) {
        const index = res.tapIndex
        if (index >= 0 && index < functions.length) {
          functions[index]()
        }
      }
    })
  }

  /**
   * 导航条加载动画
   */
  static async navigationBarLoading(duration = 500) {
    uni.showNavigationBarLoading()
    await this.timeout(duration)
    uni.hideNavigationBarLoading()
  }

  static timeout(ms) {
    return new Promise((resolve) => {
      setTimeout(resolve, ms)
    })
  }
}

/**
 * 静态变量，是否加载中
 */
Tips.isLoading = false
